const _request = require("../../utils/request.js");
const app = getApp()
Page({
  data: {
    list: [],//订单列表
    headlist: [
      {
        name: '全部',
        type: -1,
      }, {
        name: '待付款',
        type: 0,
      }, {
        name: '待发货',
        type: 1,
      }, {
        name: '待收货',
        type: 2,
      }, {
        name: '已完成',
        type: 3,
      }
    ],
    index1: -1,
  },
  onLoad: function (options) {
    if(options.type){
      this.setData({
        index1:options.type
      })
    }
  },
  onShow: function () {
    this.getlist()
  },
  // 获取订单列表接口
  getlist(){
    _request.postObj('/user/order/list', {
      status:this.data.index1>-1?this.data.index1:'',
      pageSize:1000,
      pageNo:1,
      userid:wx.getStorageSync('userid'),
    }, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        var _list = res.data.data.rows
        var _num = 0
        _list.map(items=>{
          items.orderItemList.map(inner=>{
            _num = inner.productQuantity + _num
            inner.spObj = JSON.parse(inner.sp1);
            let list = []
            inner.spObj.forEach(item => {
              list.push(item.key + ":" + item.value)
            })
            inner.spType =  list.join(',')
          })
          items.total = _num
        })
        this.setData({
          list:res.data.data.rows
        })
        console.log(this.data.list)
      }else{
        wx.showToast({
          title: res.data.message,
          icon:'none'
        })
      }
    })
  },
  // 切换头部
  gethead(e) {
    this.setData({
      index1: e.currentTarget.dataset.type
    })
    this.getlist()
  },
  // 操作
  czorder(e) {
    var _item = e.currentTarget.dataset.item
    var that = this
    if (e.currentTarget.dataset.type == 1) {//点击取消订单
      wx.showModal({
        title: '温馨提示',
        content: '确认取消订单?',
        cancelText: '取消',
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            _request.postObj('/order/updateordercancel', {
              id:_item.id,
              note:'',
            }, app.globalData.mallType).then((res) => {
              console.log(res)
              if (res.data.code === 200) {
                that.getlist()
              }else{
                wx.showToast({
                  title: res.data.message,
                  icon:'none'
                })
              }
            })
          }
        }
      })
    } 
    // else if (e.currentTarget.dataset.type == 2) { //去付款
    //   wx.navigateTo({
    //     url: '/pages/shopingmall/payment?orderId=' + _item.id + '&orderSn=' + _item.orderSn + '&realityPayPrice=' + _item.payAmount + '&createordertime=' + _item.createTime,
    //   })
    // } 
    else if (e.currentTarget.dataset.type == 3) {//提醒发货
      _request.postObj('/order/remindDelivery', {
        orderId:_item.orderSn,
        userid:wx.getStorageSync('userid'),
      }, app.globalData.mallType).then((res) => {
        console.log(res)
        if (res.data.code === 200) {
          this.getlist()
          wx.showToast({
            title: '已提醒商家发货~',
            icon:'none',
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      })
    } else if (e.currentTarget.dataset.type == 4) { //确认收货
      wx.showModal({
        title: '温馨提示',
        content: '您确认收到了吗?',
        cancelText: '取消',
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            _request.postObj('/order/confirmreceipt', {
              id:_item.id,
            }, app.globalData.mallType).then((res) => {
              console.log(res)
              if (res.data.code === 200) {
                that.getlist()
              }else{
                wx.showToast({
                  title: res.data.message,
                  icon:'none'
                })
              }
            })
          }
        }
      })
    } else if (e.currentTarget.dataset.type == 5) { //删除订单
      wx.showModal({
        title: '温馨提示',
        content: '请确认是否删除订单?',
        cancelText: '取消',
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            _request.postObj('/order/deleteorderinfo', {
              id:_item.id,
            }, app.globalData.mallType).then((res) => {
              console.log(res)
              if (res.data.code === 200) {
                that.getlist()
              }else{
                wx.showToast({
                  title: res.data.message,
                  icon:'none'
                })
              }
            })
          }
        }
      })
    }
  },
  goorderdetailed(e) {
    wx.navigateTo({
      url: '/pages/mallorder/orderinfo?orderid=' + e.currentTarget.dataset.item.id,
    })
  },
})